<?php

switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
	new_lecture_GET();
	break;

case 'POST':
	new_lecture_POST();
	break;
default:
	break;
}


function new_lecture_GET() {
	global $woodb;
	if (isset($_REQUEST['lecture_id'])) {
		$lecture_id = $_REQUEST['lecture_id'];
		$course_id = $_REQUEST['course_id'];
		$lecture = ($woodb->getLecture($lecture_id));
		$courses = ($woodb->getCourseByID($course_id));
		$lecture = $lecture[0];
		$course = $courses[0];
		if ($lecture != NULL) {
			print_edit_frame($course, $lecture);
		} else {
			?><div class="updated below-h2" id="message"><p>Lecture does not exist</p></div><?php
		}
	} else {
		?><div class="updated below-h2" id="message"><p>Invalid parameters</p></div><?php
	}
}

function new_lecture_POST() {
	global $woodb;
	$lecture_id = "";
	if (isset($_REQUEST['lecture_id'])) {
		$woodb->updateLecture($_POST['lecture_id'], $_POST['lecture_title'], $_POST['lecture_description'], $_POST['lecture_date']);
		$lecture_id = $_REQUEST['lecture_id'];
	} else if (isset($_REQUEST['course_id'])) {
		$lecture_id = $woodb->createLecture($_POST['course_id'], $_POST['lecture_title'], $_POST['lecture_description'], $_POST['lecture_date']);
	}
	$lecture = ($woodb->getLecture($lecture_id));
	$lecture = $lecture[0];
	$course_id = $_POST['course_id'];
	$course = ($woodb->getCourseByID($course_id));
	$course = $course[0];
	$_GET["course_id"]=$course->course_id;
	$_GET["lecture_id"]=$lecture->lecture_id;
	if ($lecture != NULL) {
		print_edit_frame($course, $lecture);
	}
}

function print_edit_frame($course, $lecture) {
	global $woodb;
	?><script type="text/javascript">document.body.className = document.body.className.replace('no-js','js');</script>
	<div class="wrap">
		<h2>Edit Lecture</h2>
		<div class='woo_breadcrumbs'>
		<a href='?page=woodle_admin'>Courses</a> &raquo; 
		<a href='?page=woodle_admin&action=woodle_edit_course&course_id=<?=$course->course_id?>'><?=($course->name." (".$course->semester." ".$course->year.")")?></a> &raquo; 
		Edit Lecture
		</div>
		<form method="post" action="admin.php?page=woodle_admin&action=woodle_edit_lecture&course_id=<?=$course->course_id?>&lecture_id=<?=$lecture->lecture_id?>">
			<input type="hidden" name="lecture_id" id="lecture_id" value="<?=$lecture->lecture_id?>" />
			<input type="hidden" name="course_id" id="course_id" value="<?=$course->course_id?>" />
			<div id="titlediv">
				<div id="titlewrap">
					<div style="float:left; margin-right: 20px">
						<h3>Title</h3>
						<input type="text" autocomplete="off" id="lecture_title" value="<?=$lecture->name?>" tabindex="1" size="30" name="lecture_title">
					</div>
					<div style="float:left;  margin-right: 20px">
						<h3>Date</h3>
						<input type="text" autocomplete="off" id="lecture_date" value="<?=$lecture->date?>" tabindex="1" size="30" name="lecture_date">
					</div>
				</div>
			</div>
			<div style="clear:both; padding-top: 20px">
				<h3>Course Description</h3>
				<div class="form-table">
					<?php
						$args = array("textarea_name" => "lecture_description");
						wp_editor( $lecture->description, "lecture_description", $args );
					?>
				</div>
			</div>
			<p class="submit">
			<input type="submit" class="button-primary" value="Save Lecture" />
			</p>
		</form>
	</div>
	<?php
}

?>